package com.zit.java.controller;

import cn.hutool.core.date.DateUtil;
import com.zit.java.pojo.TuitionFee;
import com.zit.java.pojo.ZhifiPayList;
import com.zit.java.service.pay.PayService;
import com.zit.java.util.object.AjaxDone;
import com.zit.java.util.object.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OtherController {


    @Autowired
    private PayService payService;


    /**
     * 支付成功跳转的页面
     * 放到数据库
     *
     * @return
     */
    @RequestMapping("/return")
    @Transactional(propagation = Propagation.REQUIRED)
    public String toReturnUrl(String out_trade_no, String total_amount, String trade_no, HttpSession session) {
        ZhifiPayList zhifiPayList = new ZhifiPayList();
        TuitionFee fee = (TuitionFee)session.getAttribute("fee");
        if (fee==null){
            throw new RuntimeException("seesion对象为空");
        }
        zhifiPayList.setOutTradeNo(out_trade_no);
        zhifiPayList.setTotalAmount(Double.parseDouble(total_amount));
        zhifiPayList.setTradeNo(trade_no);
        //订单转态
        zhifiPayList.setTradeStatus(1);
        zhifiPayList.setTradeDate(DateUtil.date());
        payService.savePayRecord(zhifiPayList);
        Integer payStatus =  fee.getId();
        payService.updStudentPayStudentStatus(payStatus);
        return "redirect:/queryPayData";
    }

    /**
     * 到查询支付的数据页面
     *
     * @return
     */
    @RequestMapping("/queryPayData")
    public String queryAllPayData() {
        return "alipay/orderlist";
    }


    /**
     * 查询数据
     */
    @RequestMapping("/queryDataList")
    @ResponseBody
    public TableData<ZhifiPayList> queryList() {
        TableData tableData = new TableData();
       List<ZhifiPayList> rstList =  payService.queryList();
        tableData.setCode(0);
        tableData.setData(rstList);
        tableData.setMsg("ok");
        tableData.setCount(rstList.size());
        return tableData;
    }


    @RequestMapping("/toFeeMag")
    public String toFeeManagementPage(){
        return "admin/tuimanage";
    }


    @RequestMapping("/feeManagement")
    @ResponseBody
    public TableData queryAllFeeData(){

        List<ZhifiPayList> zhifiPayLists = payService.queryList();
        TableData<ZhifiPayList> tb = new TableData<>();
        tb.setData(zhifiPayLists);
        return tb;
    }

    @RequestMapping("/delTrade")
    @ResponseBody
    public AjaxDone delTrade(Integer id){
       int rst =  payService.delTradeById(id);
       if (rst>0){
           return new AjaxDone(200,"删除成功！");
       }
       return new AjaxDone(300,"删除失败！");
    }

}
